<?php

namespace App\Console\Commands;

use App\Jobs\TempPush;
use App\Models\Appoint;
use App\Models\ArticleType;
use App\Models\FamilyEdu;
use App\Models\FamilyType;
use App\Models\Hospital;
use App\Models\HospitalAdministrator;
use App\Models\HospitalAdminUserHospital;
use App\Models\old\Article;
use App\Models\old\ArticleInfo;
use App\Models\old\DoctorHospital;
use App\Models\old\HospitalAdminRoleUsers;
use App\Models\old\HospitalAdminUser;
use App\Models\old\OldDoctor;
use App\Models\old\OldHospital;
use App\Models\old\OldHospitals;
use App\Models\old\UserLogin;
use App\Models\TempPushRecord;
use App\Wechat\MyAccessToken;
use App\Wechat\Temp;
use Illuminate\Console\Command;

class migrationArticle extends Command
{
    public static $typeText = [
        0 => 2,
        1 => 1,
        2 => 3,
        3 =>4,
    ];
    public static $statusText = [
        -2=>2,0 => 0, -1 => 0,1=>1
    ];

    public static $articleTypeText = [
        0 => '全部儿童',
        58 => '全部产妇',
        59 => '孕前',
        60 => '孕早期',
        61 => '孕中期',
        62 => '孕后期',
        57 => '产后',
        20 => '孕4周',
        21 => '孕5周',
        22 => '孕6周',
        23 => '孕7周',
        24 => '孕8周',
        25 => '孕9周',
        26 => '孕10周',
        27 => '孕11周',
        28 => '孕12周',
        29 => '孕13周',
        30 => '孕14周',
        31 => '孕15周',
        32 => '孕16周',
        33 => '孕17周',
        34 => '孕18周',
        35 => '孕19周',
        36 => '孕20周',
        37 => '孕21周',
        38 => '孕22周',
        39 => '孕23周',
        40 => '孕24周',
        41 => '孕25周',
        42 => '孕26周',
        43 => '孕27周',
        44 => '孕28周',
        45 => '孕29周',
        46 => '孕30周',
        47 => '孕31周',
        48 => '孕32周',
        49 => '孕33周',
        50 => '孕34周',
        51 => '孕35周',
        52 => '孕36周',
        53 => '孕37周',
        54 => '孕38周',
        55 => '孕39周',
        56 => '孕40周',
        1  => '满月',
        13 => '2月龄',
        2  => '3月龄',
        14 => '4月龄',
        15 => '5月龄',
        3  => '6月龄',
        16 => '7月龄',
        4  => '8月龄',
        17 => '9月龄',
        18 => '10月龄',
        19 => '11月龄',
        5  => '12月龄',
        6  => '18月龄',
        7  => '24月龄',
        8  => '30月龄',
        9  => '36月龄',
        10 => '4岁',
        11 => '5岁',
        12 => '6岁',
    ];
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'migration:article';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return int
     */
    public function handle()
    {
        $article = Article::query()->get();
        foreach($article as $v)
        {
            echo $v->id;
            echo "\n";
            $oldHospital = OldHospital::query()->where('hospitalid',$v->datauserid)->first();

            $row['id'] = $v->id;
            $row['type'] = self::$typeText[$v->type];
            $row['hospital_id'] = isset($oldHospital) && $v->datauserid?$oldHospital->userid:0;
            $row['status'] = self::$statusText[$v->level];
            $row['subject_pid'] = $v->subject_pid;
            $row['subject'] = $v->subject;
            $row['is_push'] = $v->type == 1?1:0;
            $row['created_at'] = date('Y-m-d H:i:s',$v->createtime);
            \App\Models\Article::query()->upsert($row,['id']);
            $info = ArticleInfo::query()->where('id',$v->id)->first();
            if($info){
                $rs['title'] = $info->title;
                $rs['ftitle'] = $info->title;
                $rs['content'] = $info->content;
                $rs['source'] = $info->source;
                $rs['img'] = str_replace('http://static.i.wedoctors.com.cn/','',$info->img);
                $rs['article_id'] = $v->id;

                \App\Models\ArticleInfo::query()->updateOrCreate($rs,['article_id'=>$v->id]);
            }
            if($v->child_type){
                $cRow['article_id'] = $v->id;
                $cRow['family_type_id'] = $v->child_type;
                ArticleType::query()->updateOrCreate($cRow,['article_id'=>$v->id,'family_type_id'=>$v->child_type]);
            }
        }
        $articleTypes = \App\Models\old\ArticleType::query()->get();
        foreach($articleTypes as $v){
            $typeTxt = self::$articleTypeText[$v->type];
            $familyType = FamilyType::query()->where('name',$typeTxt)->first();
            if(!$familyType){
                $familyType = new FamilyType();
                $familyType->name = $typeTxt;
                $familyType->save();
            }
            $cRow['article_id'] = $v->aid;
            $cRow['family_type_id'] = $familyType->id;
            ArticleType::query()->updateOrCreate($cRow,['article_id'=>$v->aid,'family_type_id'=>$familyType->id]);
        }


        return 0;
    }
}
